package com.xyht.medicine_management_system.modules.shoppingCart.controller;


import com.xyht.medicine_management_system.auth.util.JwtTokenUtil;
import com.xyht.medicine_management_system.common.commonReturn.CommonResult;
import com.xyht.medicine_management_system.common.commonReturn.ResponseResult;
import com.xyht.medicine_management_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.medicine_management_system.modules.shoppingCart.entity.req.MmsShoppingCartReq;
import com.xyht.medicine_management_system.modules.shoppingCart.service.MmsShoppingCartService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 活动表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-12-05
 */
@RestController
@SecretBody
@RequestMapping("/shoppingCart")
public class MmsShoppingCartController {
    @Resource
    private MmsShoppingCartService mmsShoppingCartService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty("添加购物车")
    @PostMapping("/add")
    public ResponseResult addShoppingCart(@RequestBody MmsShoppingCartReq shoppingCartReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsShoppingCartService.addShoppingCart(shoppingCartReq, userId);
    }

    @ApiModelProperty("删除购物车中药品")
    @PostMapping("/del")
    public ResponseResult delShoppingCart(@RequestParam("medicineId") String[] medicineId) {
        mmsShoppingCartService.removeByIds(Arrays.asList(medicineId));
        return CommonResult.success();
    }

    @ApiModelProperty("修改购物车")
    @PostMapping("/update")
    public ResponseResult updateShoppingCart(@RequestBody MmsShoppingCartReq shoppingCartReq, HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsShoppingCartService.updateShoppingCart(shoppingCartReq, userId);
    }

    @ApiModelProperty("购物车列表")
    @GetMapping("/list")
    public ResponseResult listShoppingCart(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "searchStr", required = false) String searchStr,
                                           HttpServletRequest request) {
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return mmsShoppingCartService.listShoppingCart(pageNum, pageSize, searchStr, userId);
    }

}

